package com.wz.customerserv.service.impl;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;
import com.wz.customerserv.feign.ProducerService;
import com.wz.customerserv.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private ProducerService producerService;

    public String changeValue(String name) {
        String res = "";

        res = producerService.addPrefix(name);

        return res;
    }


    @Override
    //信号量隔离的设置
    @HystrixCommand( 
        //用来设置降级方法
        fallbackMethod = "testHystrixFallback",
        commandProperties = {
            //进行熔断配置
            //条件1，设置在滚动时间窗口中，断路器的最小请求数（没有达到不会熔断）。默认20。
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold" ,value = "10"),
            //条件2，设置断路器打开的错误百分比。在滚动时间内，在请求数量超过requestVolumeThreshold的值,且错误请求数的百分比超过这个比例，断路器就为打开状态。
                                @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage" ,value = "30"),
            //条件3，设置滚动时间窗的长度，单位毫秒。这个时间窗口就是断路器收集信息的持续时间。断路器在收集指标信息的时会根据这个时间窗口把这个窗口拆分成多个桶，每个桶代表一段时间的指标，默认10000.
            @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds" ,value = "10000"),
            //设置当断路器打开之后的休眠时间，休眠时间结束后断路器为半开状态，断路器能接受请求，如果请求失败又重新回到打开状态，如果请求成功又回到关闭状态
            //单位是毫秒
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds" ,value = "3000"),
            //配置信号量隔离
            //配置信号量的数值
            @HystrixProperty(name = "execution.isolation.semaphore.maxConcurrentRequests",value = "100"),
            //选择策略为信号量隔离
            @HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE"),
            //设置HystrixCommand执行的超时时间，单位毫秒
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000000000")
        }
    )

    //线程池隔离的设置
//    @HystrixCommand(
//            groupKey="test-provider",
//            threadPoolKey="test-provider",
//            threadPoolProperties = {
//                    @HystrixProperty(name = "coreSize", value = "20"),//线程池大小
//                    @HystrixProperty(name = "maximumSize", value = "30"),//最大线程池大小
//                    @HystrixProperty(name = "maxQueueSize", value = "20"),//最大队列长度
//                    @HystrixProperty(name = "keepAliveTimeMinutes", value = "2")//线程存活时间
//            },commandProperties = {
//            @HystrixProperty(name = "execution.isolation.strategy",value = "THREAD")
//    }
//    )
    
    public String testHystrix(String name) {
        if ("FEIWU".equals(name)) {
            throw new RuntimeException();
        }

//        try {
//            Thread.sleep(4000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        return "Hello," + name;
    }

    private String testHystrixFallback(String name) {
        log.info("由于请求异常，将执行服务降级");
        return "请求异常";
    }
}
